package com.tanguy.user.utlis.extend

import android.app.Activity
import android.content.Context
import android.support.annotation.ColorRes
import android.support.annotation.DrawableRes
import android.support.annotation.IdRes
import android.view.Gravity
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import com.findme.fecedetection.base.BasePopupWindow
import com.findme.fecedetection.custom.TwoTextLinearView
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.find


/**
 * Created by dab on 2017/10/20 0020 14:04
*/
fun Activity.showPopupWindow(window: BasePopupWindow, gravity: Int = Gravity.CENTER, x: Int = 0, y: Int = 0) {
    if (this.isFinishing) {
        loge(" Activity isFinishing", 2, "err")
        return
    }
    if (window.isShowing) {
        loge(" window isShowing", 2, "err")
        return
    }
    val decorView = this.window.decorView
    if (decorView == null) {
        loge(" decorView is null", 2, "err")
        return
    }
    window.showAtLocation(this, decorView, gravity, x, y)
}

fun Activity.setText(@IdRes id: Int, string: CharSequence, @ColorRes colorRes: Int = 0): TextView {
    val find = find<TextView>(id)
    if (colorRes != 0) {
        find.setTextColor(getColorKt(colorRes))
    }
    find.text = string
    return find
}

fun Activity.setTextRight(@IdRes id: Int, string: CharSequence, @ColorRes colorRes: Int = 0): TwoTextLinearView {
    val find = find<TwoTextLinearView>(id)
    if (colorRes != 0) {
        find.setRightTextColor(getColorKt(colorRes))
    }
    find.rightText = string
    return find
}
fun Activity.setTextLeft(@IdRes id: Int, string: CharSequence, @ColorRes colorRes: Int = 0): TwoTextLinearView {
    val find = find<TwoTextLinearView>(id)
    if (colorRes != 0) {
        find.setLeftTextColor(getColorKt(colorRes))
    }
    find.setLeftText(string)
    return find
}

fun Activity.setBackground(@IdRes id: Int, @DrawableRes res: Int) {
    find<View>(id).backgroundResource = res
}

/**
 * 获取EditText商店文字,并且去除所有空格
 */
fun Activity.getEditTextString(resId: Int): String = getEditTextString(find<EditText>(resId))

fun Activity.getEditTextString(editText: EditText): String = editText.text.toString().replace(" ", "")

/**
 * 获取TwoTextLinearView右边文字,并且去除所有空格
 */
fun Activity.getTwoTextRightTextString(resId: Int): String = find<TwoTextLinearView>(resId).rightText.toString().replace(" ", "")


/**
 * 隐藏或者显示软键盘(显示键盘暂时未实现)
 */
fun Activity.hideOrShowKeyboard(hide: Boolean) {
    if (hide) {
        val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(this.currentFocus.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    } else {
        throw IllegalAccessException("未实现显示软键盘的方法")
    }

}



/**
 * 修改屏幕的透明度
 */
fun Activity.changeWindowAlpha(alpha: Float) {
    if (window == null) {
        loge("changeWindowAlpha: window == null")
        return
    }

    val lp = window.attributes
    lp.alpha = alpha
    window.attributes = lp

}